#include "RsGisLibFilterGaussian1stDeriv.h"
#include "ExtensionManager.h"
#include "ExtensionSession.h"
#include <QVariant>

RsGisLibFilterGaussian1stDeriv::RsGisLibFilterGaussian1stDeriv(RsgisPortal* ClientPortal, QWidget *parent)
{
	ui = new Ui::RsGisLibFilterGaussian1stDeriv();
	ui->setupUi(this);

	Portal = ClientPortal;

	connect(ui->pushButton_InputFilePath, SIGNAL(clicked()), this, SLOT(on_action_Button_InputFilePath_clicked()));
	connect(ui->pushButton_OutputFilePath, SIGNAL(clicked()), this, SLOT(on_action_Button_OutputFilePath_clicked()));
	connect(ui->pushButton_ApplyFilter, SIGNAL(clicked()), this, SLOT(on_action_Button_ApplyFilter_clicked()));

	ui->textBrowser_ParameterDescription->setText(
		"Filter Size - odd int specifying the size of the image filter\nFilter Angle - the angle of the filter\nStddev X - standard deviation in the x-axis\nStddev Y - standard deviation in the y-axis\nGdal Format - string specifying the output image format\nDatatype - Specifying the output image pixel data type"
	);

	ui->textBrowser_ParameterDescription->setEnabled(false);

	QStringList DatatypeList;
	DatatypeList.append("undefined");
	DatatypeList.append("8 Int");
	DatatypeList.append("16 Int");
	DatatypeList.append("32 Int");
	DatatypeList.append("64 Int");
	DatatypeList.append("8 uInt");
	DatatypeList.append("16 uInt");
	DatatypeList.append("32 uInt");
	DatatypeList.append("64 uInt");
	DatatypeList.append("32 Float");
	DatatypeList.append("64 Float");
	ui->comboBox_Datatype->addItems(DatatypeList);

	ui->lineEdit_InputFilePath->setText("D:\\RS-RPC\\TEST\\IN\\RsGisLib\\Test.tif");
	ui->lineEdit_OutputFilePath->setText("D:\\RS-RPC\\TEST\\IN\\RsGisLib\\TestGaussian1stDeriv");

	ui->lineEdit_FilterSize->setText("3");
	ui->lineEdit_FilterAngle->setText("0.0");
	ui->lineEdit_StddevX->setText("1.0");
	ui->lineEdit_StddevY->setText("1.0");
	ui->lineEdit_GdalFormat->setText("GTIFF");
	ui->comboBox_Datatype->setCurrentIndex(10);

	ui->lineEdit_Priority->setText("16");
}

void RsGisLibFilterGaussian1stDeriv::on_action_Button_InputFilePath_clicked()
{
	QString str = QFileDialog::getOpenFileName(this, "File Explorer", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), "Tif file(*.tif *.TIF);;All file(*.*)");
	ui->lineEdit_InputFilePath->setText(str.toUtf8());
}

void RsGisLibFilterGaussian1stDeriv::on_action_Button_OutputFilePath_clicked()
{
	QString str = QFileDialog::getSaveFileName(this, "File Explorer", QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation), "Tif file(*.tif *.TIF);;All file(*.*)");
	ui->lineEdit_OutputFilePath->setText(str.toUtf8());
}

void RsGisLibFilterGaussian1stDeriv::on_action_Button_ApplyFilter_clicked()
{
	{
		if (ui->lineEdit_InputFilePath->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Input File Path is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_OutputFilePath->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Output File Path is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_FilterSize->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Filter Size is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		int Tmp = ui->lineEdit_FilterSize->text().toInt();

		if (Tmp % 2 != 1)
		{
			QMessageBox::warning(NULL, "Error", "Filter Size is not Odd", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_FilterAngle->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Filter Angle is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_StddevX->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Stddev X is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_StddevY->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Stddev Y is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_GdalFormat->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Gdal Format is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}

		if (ui->lineEdit_Priority->text().isEmpty())
		{
			QMessageBox::warning(NULL, "Error", "Priority is Null", QMessageBox::Yes, QMessageBox::Yes);
			return;
		}
	}

	QList<QVariant> SysParaList;
	SysParaList << QVariant(QString("RsGisLib"));
	SysParaList << QVariant(QString("executeFilter"));
	SysParaList << QVariant(int(ui->lineEdit_Priority->text().toInt()));

	QList<QVariant> AlgParaList;
	AlgParaList << QVariant(QString("Gaussian1st"));
	AlgParaList << QVariant(QString(ui->lineEdit_InputFilePath->text()));
	AlgParaList << QVariant(QString(ui->lineEdit_OutputFilePath->text()));
	AlgParaList << QVariant(int(ui->lineEdit_FilterSize->text().toInt()));
	AlgParaList << QVariant(double(ui->lineEdit_StddevX->text().toDouble()));
	AlgParaList << QVariant(double(ui->lineEdit_StddevY->text().toDouble()));
	AlgParaList << QVariant(double(ui->lineEdit_FilterAngle->text().toDouble()));
	AlgParaList << QVariant(QString(ui->lineEdit_GdalFormat->text()));
	AlgParaList << QVariant(int(ui->comboBox_Datatype->currentIndex()));

	QVariantList ArgList;
	ArgList << (QVariant)SysParaList;
	ArgList << (QVariant)AlgParaList;

	Portal->goAhead(ArgList);
}